package com.evildog.workspace.system.application.controller;


import com.evildog.workspace.common.object.PageQuery;
import com.evildog.workspace.common.result.PagedResult;
import com.evildog.workspace.common.result.Result;
import com.evildog.workspace.common.result.ResultGenerator;
import com.evildog.workspace.dependencies.mybatis.util.QueryConditionUtils;
import com.evildog.workspace.system.application.bo.ApiPermissionCategoryPageBO;
import com.evildog.workspace.system.application.convert.ApiPermissionCategoryConvert;
import com.evildog.workspace.system.application.model.ApiPermissionCategoryPageParam;
import com.evildog.workspace.system.application.model.ApiPermissionCategoryParam;
import com.evildog.workspace.system.application.service.ApiPermissionCategoryService;
import com.evildog.workspace.system.application.vo.ApiPermissionCategoryPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * apiPermissionCategory Controller
 * api权限分类
 * </p>
 *
 * @author evildog
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/api-permission-categories")
@AllArgsConstructor
@Api(tags = "apiPermissionCategory相关接口")
public class ApiPermissionCategoryController {

    private ApiPermissionCategoryService apiPermissionCategoryService;

    @PostMapping
    @ApiOperation(value = "添加接口")
    public Result<Boolean> save(@RequestBody @Validated  ApiPermissionCategoryParam apiPermissionCategoryParam) {
        return ResultGenerator.status(apiPermissionCategoryService.save(ApiPermissionCategoryConvert.INSTANCE.convert(apiPermissionCategoryParam)));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public Result<Boolean> removeById(@PathVariable("id") Long id) {
        return ResultGenerator.status(apiPermissionCategoryService.removeById(id));
    }

    @PutMapping("/{id}")
    @ApiOperation(value = "更新接口")
    public Result<Boolean> updateById(@PathVariable("id") Long id, @RequestBody @Validated ApiPermissionCategoryParam apiPermissionCategoryParam) {
        return ResultGenerator.status(apiPermissionCategoryService.updateById(id, ApiPermissionCategoryConvert.INSTANCE.convert(apiPermissionCategoryParam)));
    }

    @GetMapping("/page")
    @ApiOperation(value = "表分页查询接口")
    public Result<PagedResult<ApiPermissionCategoryPageVO>> getPageList(ApiPermissionCategoryPageParam apiPermissionCategoryPageParam, PageQuery query) {
        PagedResult<ApiPermissionCategoryPageBO> apiPermissionCategoryPageBO = apiPermissionCategoryService.getPageList(
                query,
                QueryConditionUtils.getQueryCondition(apiPermissionCategoryPageParam),
                ApiPermissionCategoryConvert.INSTANCE.convert(apiPermissionCategoryPageParam));
        return ResultGenerator.success(ApiPermissionCategoryConvert.INSTANCE.convert(apiPermissionCategoryPageBO));
    }

    @GetMapping
    @ApiOperation(value = "查询可用API分类")
    public Result<List<ApiPermissionCategoryPageVO>> getValidList() {
        List<ApiPermissionCategoryPageBO> apiPermissionCategoryPageBO = apiPermissionCategoryService.getValidList();
        return ResultGenerator.success(ApiPermissionCategoryConvert.INSTANCE.convert(apiPermissionCategoryPageBO));
    }

}